package com.zuipin.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zuipin.entity.NewsInfo;
import com.zuipin.entity.NewsInfoImg;
import com.zuipin.entity.SysUser;
import com.zuipin.entity.product.ProductCollection;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.result.PageResult;
import com.zuipin.framework.result.Result;
import com.zuipin.mapper.NewsInfoMapper;
import com.zuipin.service.IBaseDictionaryDataService;
import com.zuipin.service.INewsInfoImgService;
import com.zuipin.service.INewsInfoService;
import com.zuipin.service.IProductCollectionService;
import com.zuipin.service.ISysUserService;
import com.zuipin.util.DateUtils;
import com.zuipin.util.Pagination;
import com.zuipin.vo.BackNewsInfoDetailVo;
import com.zuipin.vo.BackNewsInfoVo;
import com.zuipin.vo.NewsInfoListVo;
import com.zuipin.vo.NewsInfoSearchVo;

/**
 * 新闻信息实现
 * @author zhouhuihang
 *
 */
@Service
public class NewsInfoServiceImpl implements INewsInfoService {
	@Resource
	private NewsInfoMapper				newsInfoMapper;
	@Resource
	private IBaseDictionaryDataService	baseDictionaryDataService;
	@Resource
	private ISysUserService				sysUserService;
	@Resource
	private INewsInfoImgService			newsInfoImgService;
	@Resource
	private IProductCollectionService	productCollectionService;
	
	/**
	 * (后台)查询全部新闻信息
	 */
	@Override
	@Transactional(rollbackFor = { Exception.class })
	public Result<List<BackNewsInfoVo>> searchNewsInfoAllByBack(NewsInfoSearchVo search, Pagination page) {
		PageResult<List<BackNewsInfoVo>> result = new PageResult<List<BackNewsInfoVo>>();
		List<BackNewsInfoVo> data = newsInfoMapper.searchNewsInfoAllByBack(search, page);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setData(data);
		result.setTotal_record(page.getTotalRecord());
		return result;
	}
	
	/**
	 * 
	 * 新增新闻信息
	 * 
	 */
	@Override
	@Transactional(rollbackFor = { Exception.class })
	public Result creatNewsInfo(BackNewsInfoDetailVo newsInfoDetail) {
		Result result = new Result();
		NewsInfo newsInfo = new NewsInfo();
		// 默认非热讯
		newsInfo.setHotNewsTag(0);
		BeanUtils.copyProperties(newsInfoDetail, newsInfo);
		newsInfo.setDelFlag(0);
		/*if (newsInfoDetail.getTaggingId() != null) {
			BaseDictionaryData baseDictionaryData = baseDictionaryDataService.findById(newsInfoDetail.getTaggingId());
			if (baseDictionaryData != null) {
				newsInfo.setKeyword("醉品商城，茶叶商城，" + baseDictionaryData.getDataName());
			}
			
		}*/
		/*if (BackNewsInfoDetailVo.NEWS_HEADLINES.equals(newsInfoDetail.getNewsTypeId())) {
			
		}*/
		newsInfo.setNewsStatus(0);
		newsInfo.setCreatedTime(DateUtils.getCurrentDateTime());
		newsInfo.setOperatingTime(DateUtils.getCurrentDateTime());
		SysUser sysUser = sysUserService.findById(newsInfoDetail.getCreateId());
		if (sysUser != null) {
			newsInfo.setCreator(sysUser.getRealName());
			newsInfo.setOperator(sysUser.getRealName());
		}
		// (插入数据库)乱码解决===========================
		
		// ====================================
		newsInfoMapper.insertSelective(newsInfo);
		// 新建新闻相册
		List<NewsInfoImg> newsInfoImgs = newsInfoDetail.getNewsInfoImgs();
		for (NewsInfoImg newsInfoImg : newsInfoImgs) {
			newsInfoImg.setCreateId(newsInfo.getCreateId());
			newsInfoImg.setCreateTime(DateUtils.getCurrentDateTime());
			newsInfoImg.setDelFlag(0);
			newsInfoImg.setNewsInfoId(newsInfo.getId());
			newsInfoImgService.insert(newsInfoImg);
		}
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setMessage("新建成功！");
		return result;
	}
	
	/**
	 * 
	 * 修改新闻信息
	 * 
	 */
	@Transactional(rollbackFor = { Exception.class })
	public Result updateNewsInfo(BackNewsInfoDetailVo newsInfoDetail) {
		Result result = new Result();
		NewsInfo oldnewsInfo = newsInfoMapper.selectByPrimaryKey(newsInfoDetail.getId());
		if (oldnewsInfo == null || oldnewsInfo.getDelFlag().equals(NewsInfo.DELFLAG_1)) {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setMessage("新闻不存在！");
			return result;
		}
		NewsInfo newsInfo = new NewsInfo();
		// 默认非热讯
		newsInfo.setHotNewsTag(0);
		BeanUtils.copyProperties(newsInfoDetail, newsInfo);
		/*	if (BackNewsInfoDetailVo.NEWS_HEADLINES.equals(newsInfoDetail.getNewsTypeId())) {
				newsInfo.setHotNewsTag(0);
			}*/
		/*newsInfo.setKeyword("");
		if (newsInfoDetail.getTaggingId() != null) {
			BaseDictionaryData baseDictionaryData = baseDictionaryDataService.findById(newsInfoDetail.getTaggingId());
			if (baseDictionaryData != null) {
				newsInfo.setKeyword("醉品商城，茶叶商城，" + baseDictionaryData.getDataName());
			}
		}*/
		SysUser sysUser = sysUserService.findById(newsInfoDetail.getUpdateId());
		if (sysUser != null) {
			newsInfo.setOperator(sysUser.getRealName());
		}
		newsInfo.setOperatingTime(DateUtils.getCurrentDateTime());
		newsInfoMapper.updateBackNewsInfoDetail(newsInfo);
		
		// 删除之前新闻相册
		newsInfoImgService.deleteAllByNewsInfoId(newsInfoDetail.getId());
		// 新建新闻相册
		List<NewsInfoImg> newsInfoImgs = newsInfoDetail.getNewsInfoImgs();
		for (NewsInfoImg newsInfoImg : newsInfoImgs) {
			newsInfoImg.setCreateId(newsInfo.getCreateId());
			newsInfoImg.setCreateTime(DateUtils.getCurrentDateTime());
			newsInfoImg.setDelFlag(0);
			newsInfoImg.setNewsInfoId(newsInfo.getId());
			newsInfoImgService.insert(newsInfoImg);
		}
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setMessage("保存成功！");
		return result;
	}
	
	/**
	 * 
	 * 查看新闻详细信息
	 * 
	 */
	@Transactional(rollbackFor = { Exception.class })
	public Result<BackNewsInfoDetailVo> findNewsInfoDetail(Long sysId, Long id, Short isIncreaseRealNum) {
		Result result = new Result();
		String conetent = "";
		BackNewsInfoDetailVo newsInfoDetail = newsInfoMapper.findNewsInfoDetail(sysId, id);
		if (newsInfoDetail == null) {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setMessage("没有查询到新闻信息");
			return result;
		}
		// 查询集合名称
		ProductCollection productCollection = productCollectionService.selectByPrimaryKey(newsInfoDetail.getProductCollectionId());
		if (productCollection != null && productCollection.getDelFlag().equals(false)) {
			newsInfoDetail.setProductCollectionName(productCollection.getCollectionName());
			newsInfoDetail.setProductCollectionNo(productCollection.getCollectionNo());
		}
		// 查询新闻相册
		NewsInfoImg seach = new NewsInfoImg();
		seach.setDelFlag(0);
		seach.setNewsInfoId(id);
		List<NewsInfoImg> list = newsInfoImgService.findNewsInfoImg(seach);
		// 判断是否增加真实阅读量
		if (BackNewsInfoDetailVo.IS_INCREASE_REAL_NUM_1.equals(isIncreaseRealNum)) {
			// 增加
			newsInfoMapper.addRealReadNum(id);
		}
		newsInfoDetail.setNewsInfoImgs(list);
		result.setData(newsInfoDetail);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setMessage("查询成功");
		return result;
	}
	
	/**
	 * 
	 * (前台)查看新闻列表
	 * 
	 */
	@Transactional(rollbackFor = { Exception.class })
	public Result<List<NewsInfoListVo>> getNewsInfoList(Long sysId, Integer hotNewsTag, Long newsTypeId, Long taggingId, Pagination page) {
		PageResult<List<NewsInfoListVo>> result = new PageResult<List<NewsInfoListVo>>();
		List<NewsInfoListVo> data = newsInfoMapper.getNewsInfoList(sysId, hotNewsTag, newsTypeId, taggingId, page);
		// set 新闻相册list
		for (NewsInfoListVo newsInfo : data) {
			NewsInfoImg seach = new NewsInfoImg();
			seach.setDelFlag(0);
			seach.setNewsInfoId(newsInfo.getId());
			List<NewsInfoImg> list = newsInfoImgService.findNewsInfoImg(seach);
			newsInfo.setNewsInfoImgs(list);
		}
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setData(data);
		result.setMessage("查询成功");
		result.setTotal_record(page.getTotalRecord());
		return result;
	}
	
	/**
	 * 
	 * 显示|隐藏新闻
	 * 
	 */
	@Override
	public Result modifyNewsInfoStatus(Long id, Integer newsStatus) {
		Result result = new Result();
		NewsInfo oldNewsInfo = newsInfoMapper.selectById(id);
		if (oldNewsInfo == null) {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setMessage("新闻不存在");
			return result;
		}
		if (NewsInfo.DISPLAY_1.equals(oldNewsInfo.getNewsStatus()) && NewsInfo.HIDE_0.equals(newsStatus)) {
			newsInfoMapper.modifyNewsInfoStatus(id, newsStatus);
			result.setErrorCode(ErrorCode.SUCCESS);
			result.setMessage("隐藏成功");
			return result;
		}
		if (NewsInfo.HIDE_0.equals(oldNewsInfo.getNewsStatus()) && NewsInfo.DISPLAY_1.equals(newsStatus)) {
			newsInfoMapper.modifyNewsInfoStatus(id, newsStatus);
			result.setErrorCode(ErrorCode.SUCCESS);
			result.setMessage("显示成功");
			return result;
		}
		result.setErrorCode(ErrorCode.ELSE);
		result.setMessage("无法执行显示/隐藏");
		return result;
	}
	
}
